function [R] = gen_r(un, lambda, ep, nx, ny)
hx = 1/nx;
hy = 1/ny;
length = (nx+1)*(ny+1);
jaco = zeros(length);
for k=1:length
    [i, j] = ij(k,nx);
    if (i==1)|(i==nx+1)|(j==1)|(j==ny+1)
        un(k) = 0;
    end
end

for k=1:length
    [i, j] = ij(k,nx);   

        if j < ny+1        %make sure it isn't the top row
           jaco(k, l(i,j+1,nx)) = 1/hy^2;   
        end
        if i < nx+1             % make sure it isn't the right side
            jaco(k, l(i+1,j,nx)) = 1/hx^2;
        end

        jaco(k,k) = -2/hx^2 -2/hy^2;%+lambda;

        if 1 < i                % make sure it isn't the left side
            jaco(k, l(i-1,j, nx)) = 1/hx^2;
        end
        if 1 < j                % make sure it isn't the bottom row
            jaco(k, l(i,j-1,nx)) = 1/hy^2;
        end

end
R = -jaco*un +lambda*(un+un.^2) + ep.*gen_f(nx,ny);
for k=1:length
    [i, j] = ij(k,nx);
    if (i==1)|(i==nx+1)|(j==1)|(j==ny+1)
     %   R(k) = 0;
    end
end

        
    